﻿<?php

$obj=new mysql($config);



if(!empty($_POST)){
    $arr=array();
    $id=$_POST['id'];


    //有展示图上传则删除原图，从新上传新的图片
   if(!empty($_FILES['orignalimg']['name'])){
            $sql="select `orignalimg`,`thumbimg` from `goods` where `id`=$id";
            $date=$obj->getOne($sql);
            if(!empty($date)){
                       unlink('./'.$date['orignalimg']);
                       unlink('./'.$date['thumbimg']);
            }

            $orignalimg=upload($_FILES['orignalimg']);
            $thumbimg=thumb($orignalimg);
   }

    if(isset($orignalimg)&&!empty($orignalimg)){
            $arr['orignalimg']=$orignalimg;
            $arr['thumbimg']=$thumbimg;
    }



    // 其他图片上传
     if(isset($_FILES['new_img'])){
               $other=checkimg($_FILES['new_img']);
               if(!empty($other['name'][0])){
                           $arrlist=array();
                           foreach ($other['name'] as $k => $v) {
                               $arrlist[$k]['name']=$other['name'][$k];
                               $arrlist[$k]['type']=$other['type'][$k];
                               $arrlist[$k]['size']=$other['size'][$k];
                               $arrlist[$k]['tmp_name']=$other['tmp_name'][$k];
                               $arrlist[$k]['error']=$other['error'][$k];
                           }

                           $goodsimg=array();
                           $goodsimg['goods_id']=$id;
                           foreach ($arrlist as $k => $v) {
                                $orignal_img=upload($v);
                                $thumb_img=thumb($orignal_img);
                                $goodsimg['orignal_img']=$orignal_img;
                                $goodsimg['thumb_img']=$thumb_img;
                                $obj->insert($goodsimg,'goodsimg');
                           }

               }
     }





     //修改当前的其它图片
    $new=checkimg($_FILES['orignal_img']);
    if(!empty($new['name'])){
                $arrlist=array();
                foreach ($new['name'] as $k => $v) {
                   $arrlist[$k]['name']=$new['name'][$k];
                   $arrlist[$k]['type']=$new['type'][$k];
                   $arrlist[$k]['size']=$new['size'][$k];
                   $arrlist[$k]['tmp_name']=$new['tmp_name'][$k];
                   $arrlist[$k]['error']=$new['error'][$k];
                }

                foreach ($arrlist as $k => $v) {
                 $sql="select `orignal_img`,`thumb_img` from `goodsimg` where `id`=$k";
                 $date=$obj->getOne($sql);
                 unlink('./'.$date['orignal_img']);
                 unlink('./'.$date['thumb_img']);
                 $obj->del("id=$k",'goodsimg');
                 $orignal_img=upload($v);
                 $thumb_img=thumb($orignal_img);
                 $obj->insert(array('goods_id'=>$id,'orignal_img'=>$orignal_img,'thumb_img'=>$thumb_img),'goodsimg');
                }
    }




    // 新增属性
           if(isset($_POST['attr_id'])){
                    $i=0;
                    foreach ($_POST['attr_id'] as $k => $v) {
                           if(!empty($v)){
                                   if(!is_array($v)){

                                        $obj->insert(array('goods_id'=>$id,'attr_id'=>$k,'attr_value'=>$v),'goodsattr');
                                   }else{
                                        foreach ($v as $k1 => $v1) {
                                        $obj->insert(array('goods_id'=>$id,'attr_id'=>$k,'attr_value'=>$v1,'price'=>$_POST['prices'][$i]),'goodsattr');
                                        $i++;
                                        }
                                   }
                           }

                    }
           }



    // 修改属性
    $i=0;
    foreach ($_POST['oldattr_id'] as $k => $v) {
            if(!is_array($v)){
                 $obj->update(array('attr_value'=>$v),array('id'=>$k),'goodsattr');
            }else{

                foreach ($v as $k1 => $v1) {
                    $obj->update(array('attr_value'=>$v1,'price'=>$_POST['oldprices'][$i]),array('id'=>$k1),'goodsattr');
                    $i++;
                }

            }
    }




    $arr['cid']=$_POST['cid'];
    $arr['goods_name']=$_POST['goods_name'];
    $arr['price']=$_POST['price'];
    $arr['des']=$_POST['des'];


    $row=$obj->update($arr,array('id'=>$id),'goods');



    if($row>-1){
        echo '<script>alert("更新成功");location.href="index.php?c=goods&a=list&mod=admin";</script>';
    }else{
        echo '<script>alert("更新失败");history.go(-1);</script>';
    }







}






    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='delimg'){
        $id=$_GET['imgid'];
        $sql="select `orignal_img`,`thumb_img` from `goodsimg` where `id`=$id";
        $dates=$obj->getOne($sql);
        unlink('./'.$dates['orignal_img']);
        unlink('./'.$dates['thumb_img']);
        $row=$obj->del("id=$id",'goodsimg');
        echo $row;
        die;
    }

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='delattr'){
        $id=$_GET['id'];
        $row=$obj->del("id=$id",'goodsattr');
        echo $row;
        die;
    }















$id=$_GET['id'];
$sql="select * from `goods` where id=$id";
$date=$obj->getOne($sql);

//查找出商品的分类
$sql1="select * from `cate` where `cate_type`=2";  //cate_type等于2代表的是分类产品类
$dates=$obj->getCondition($sql1);
$cates=getc($dates,0);


//查找出goodsimg表中对应商品id的图片
$sql1="select * from `goodsimg` where `goods_id`=$id";  //cate_type等于2代表的是分类产品类
$dates1=$obj->getCondition($sql1);


// 查找出商品的全部属性,并查找出该商品对应的属性
$sql1="select * from `attr`";
$attr=$obj->getCondition($sql1);

$sql1="select * from `goodsattr` where `goods_id`=$id";
$_goodsattr=$obj->getCondition($sql1);
$goodsattr=array();
foreach ($_goodsattr as $k => $v) {
    $goodsattr[$v['attr_id']][]=$v;
}


require_once (VIEW_PATH.'admin/layout.html');

